﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manager_user : System.Web.UI.Page
{
    private string TABLE_CANDIDATE = "Candidate";
    private string txtEmail, txtPass, txtCPass, txtName, txtAddress, txtCity, txtCountry, txtPhone, txtDescription;
    private string txtBirthday, txtAvatar;
    private SQLQuery sqlQuery = null;
    private Connection myConnection = null;
    
    private string path = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("../index.htm");
        }
        myConnection = new Connection();
        if (!myConnection.isConnectionDatabase())
        {
            Response.Redirect("../disconnect.htm");
        }

        path = Server.MapPath(".") + "\\..//image//avatar//candidate//";
        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {
            fillDataToGridView();
            showGridView();
        }
    }

    private void showGridView()
    {
        layout_gridview.Visible = true;
        layout_infor.Visible = false;
    }

    private void showLayoutInfor()
    {
        layout_gridview.Visible = false;
        layout_infor.Visible = true;
    }

    private void fillDataToGridView()
    {
        sqlQuery.loadDataGridView(mGridView, LocalVariables.SQLQUERY_SELECT_TABLE_CANDIDATE);
    }

    protected void mGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView.PageIndex = e.NewPageIndex;
        fillDataToGridView();
    }

    protected void mGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        showLayoutInfor();
        txt_email.Enabled = false;
        btn_save.Visible = false;
        btn_reset.Visible = false;
        btn_edit.Visible = true;
        txtEmail = mGridView.SelectedRow.Cells[1].Text.ToString().Trim();
        txt_email.Text = txtEmail;
        string[] arrayString = sqlQuery.selectInforItemCandidate(txtEmail);
        txt_name.Text = arrayString[0].ToString();
        txt_address.Text = arrayString[1].ToString();
        txt_city.Text = arrayString[2].ToString();
        txt_country.Text = arrayString[3].ToString();
        if (arrayString[4].ToString().Trim() != "0")
        {
            txt_phone.Text = arrayString[4].ToString();
        }
        ftb_description.Text = arrayString[5].ToString();
        bdp_birthday.SelectedValue = arrayString[6].ToString();
        if (LocalVariables.isNullorEmpty(arrayString[7].ToString().Trim()))
        {
            img_avatar.ImageUrl = "../image/manager/no_avatar.jpg";
        }
        else
        {
            img_avatar.ImageUrl = "../image/avatar/candidate/" + arrayString[7].ToString();
        }
    }

    private bool getText()
    {
        txtEmail = txt_email.Text.Trim();
        txtPass = txt_password.Text.Trim();
        if (txtPass.Length > 0)
        {
            if (txtPass.Length < 6)
            {
                lbl_msg.Text = LocalVariables.PASSWORD_CHARACTERS;
                return false;
            }
            txtCPass = txt_cpassword.Text.ToString().Trim();
            if (txtPass != txtCPass)
            {
                lbl_msg.Text = LocalVariables.CPASSWORD_FAILURE;
                return false;
            }
        }
        txtName = txt_name.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtName))
        {
            lbl_msg.Text = LocalVariables.NAME_FAILURE;
            return false;
        }
        txtAddress = txt_address.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtAddress))
        {
            lbl_msg.Text = LocalVariables.ADDRESS_FAILURE;
            return false;
        }
        txtCity = txt_city.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtCity))
        {
            lbl_msg.Text = LocalVariables.CITY_FAILURE;
            return false;
        }
        txtCountry = txt_country.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtCountry))
        {
            lbl_msg.Text = LocalVariables.COUNTRY_FAILURE;
            return false;
        }
        txtPhone = txt_phone.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtPhone))
        {
            txtPhone = "0";
        }
        txtDescription = ftb_description.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtDescription))
        {
            lbl_msg.Text = LocalVariables.DESCRIP_FAILURE;
            return false;
        }
        txtBirthday = bdp_birthday.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtBirthday))
        {
            lbl_msg.Text = LocalVariables.BIRTHDAY_FAILURE;
            return false;
        }
        return true;
    }

    private void clearText()
    {
        txt_email.Text = "";
        txt_password.Text = "";
        txt_cpassword.Text = "";
        txt_name.Text = "";
        txt_address.Text = "";
        txt_city.Text = "";
        txt_country.Text = "";
        txt_phone.Text = "";
        ftb_description.Text = "";
        bdp_birthday.SelectedValue = "";
    }

    
    protected void btn_add_Click(object sender, EventArgs e)
    {
        clearText();
        showLayoutInfor();
        btn_edit.Visible = false;
        btn_save.Visible = true;
        btn_reset.Visible = true;
        txt_email.Enabled = true;
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            if (sqlQuery.selectEmailIsExits(txtEmail, TABLE_CANDIDATE))
            {
                lbl_msg.Text = LocalVariables.EMAIL_BEEN_USED;
                return;
            }
            else
            {
                LocalVariables.SAVE_VALUE_AVATAR = "";
                if (file_upload.HasFile)
                {
                    LocalVariables.SAVE_VALUE_AVATAR = LocalVariables.getFileName("", file_upload.FileName);
                    file_upload.SaveAs(path + LocalVariables.SAVE_VALUE_AVATAR);
                }
                if (sqlQuery.insertTableCandidate(txtEmail, txtPass, txtName, txtBirthday, txtAddress, txtCity, txtCountry, txtDescription, txtPhone, LocalVariables.SAVE_VALUE_AVATAR))
                {
                    lbl_not_data.Text = LocalVariables.CANDIDATE_USER_SUCCESS;
                    showGridView();
                    fillDataToGridView();
                }
            }
        }        
    }

    protected void btn_edit_Click(object sender, EventArgs e)
    {
        if (getText())
        {
            LocalVariables.SAVE_VALUE_AVATAR = "";
            if (file_upload.HasFile)
            {
                LocalVariables.SAVE_VALUE_AVATAR = LocalVariables.getFileName("", file_upload.FileName);
                file_upload.SaveAs(path + LocalVariables.SAVE_VALUE_AVATAR);
            }
            if (sqlQuery.updateTableCandidate(txtEmail, txtPass, txtName, txtBirthday, txtAddress, txtCity, txtCountry, txtDescription, txtPhone, LocalVariables.SAVE_VALUE_AVATAR))
            {
                showGridView();
                fillDataToGridView();
                lbl_not_data.Text = LocalVariables.EDIT_PROFILE_USER;
            }
        }
    }

    protected void btn_reset_Click(object sender, EventArgs e)
    {
        clearText();
    }

    protected void btn_back_Click(object sender, EventArgs e)
    {
        showGridView();
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlInputCheckBox chk = new HtmlInputCheckBox();
            for (int i = 0; i < mGridView.Rows.Count; i++)
            {
                chk = (HtmlInputCheckBox)mGridView.Rows[i].FindControl("chk");
                if (chk.Checked == true)
                {
                    sqlQuery.deleteTable(TABLE_CANDIDATE, "[email]", chk.Value.ToString());
                }
            }
            fillDataToGridView();
        }
        catch { }
    }

    protected void btn_refresh_Click(object sender, EventArgs e)
    {
        lbl_not_data.Text = "";
        txt_search.Text = "";
        fillDataToGridView();
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        string value = txt_search.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(value))
        {
            lbl_not_data.Text = LocalVariables.KEYWORD_FAILURE;
            return;
        }
        lbl_not_data.Text = "";
        if (sqlQuery.searchInforTableCandidate(mGridView, value))
        {
            lbl_not_data.Text = LocalVariables.SEARCH_NOT_RESULTS;
        }
    }
}